~function () {
  const md="compiler"
  function warpResp(data, url, type = 'application/javascript') {
    let resp = new Response(new Blob([data], { type: type }));
    return {
      url, ok: true,
      headers: {
        get(header) {
          return {
            'content-type': type
          }[header]
        }
      },
      type: 'basic',
      text: () => resp.text()
    }
  }
  function createCss(src) {
    let style = document.createElement('link')
    style.href = src
    style.rel = 'stylesheet'
    style.type = 'text/css'
    document.head.appendChild(style)
    return new Response(new Blob([''], { type: 'application/javascript' }));
  }
  window.esmsInitOptions = {
    shimMode: true,
    mapOverrides: true,
    revokeBlobURLs: true,
    fetch: async function (url, options) {
      // try{
        if (url.endsWith('.css')) {
          return createCss(url)
         // return null
        }
        const res = await fetch(url, options);
        if (!res.ok) {
          return res;
        }
        const contentType = res.headers.get("content-type") || ""
        if (contentType.indexOf('javascript') > -1) {
          return res;
        } else if (contentType.indexOf('text/css') > -1) {
          return createCss(url)
        }
        if (res.url.endsWith('.ts')) {
          const source = await res.text();
          const compiler = await importShim(md)
          const transformed = compiler.tsc(source, {
            experimentalDecorators: true,
              declaration:true,
              module:'ES2015',
              jsx: "react",
              jsxFactory: "h",
              jsxFragmentFactory: "Fragment"
          });
          return warpResp(transformed, url)
        } else if (res.url.endsWith('.vue')) {
          const source = await res.text();
          const compiler = await importShim(md)
          const transformed = compiler.sfc(source);
          return warpResp(transformed, url)
        } else if(res.url.endsWith('.less')){
          const source = await res.text();
          const compiler = await importShim(md)
          const transformed = compiler.lessc(source);
          let style = document.createElement('style')
          style.innerHTML=transformed
          document.head.appendChild(style)
          return new Response(new Blob([''], { type: 'application/javascript' }));
        }
        return res;
      // }catch(e){
      //
      //   //console.error(url,e);
      //   //return new Response(new Blob(['export default {}'], { type: 'application/javascript' }));
      // }
    }
  }
}()
